package net.phoenix.repository.property;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.nodetype.PropertyDefinition;

import net.phoenix.repository.BasicNode;
import net.phoenix.repository.value.ReferenceValue;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class ReferenceProperty extends BasicProperty {
	/**
	 * create single value property
	 * 
	 * @param node
	 * @param definition
	 * @param value
	 */
	public ReferenceProperty(BasicNode node, PropertyDefinition definition, Value value) {
		super(node, definition, value);

	}

	/**
	 * create multiple values property
	 * 
	 * @param node
	 * @param definition
	 * @param values
	 */
	public ReferenceProperty(BasicNode node, PropertyDefinition definition, Value[] values) {
		super(node, definition, values);
	}

	/**
	 * create a property with default value;
	 * 
	 * @param node
	 * @param definition
	 */
	public ReferenceProperty(BasicNode node, PropertyDefinition definition) {
		super(node, definition);

	}
	

	@Override
	public Node getNode() throws IllegalStateException, RepositoryException {
		if (this.values.length == 0)
			return null;
		return this.getParent().getSession().getNodeByIdentifier(((ReferenceValue) this.getValue()).getString());
	}

	@Override
	public Property getProperty() throws ValueFormatException, IllegalStateException, RepositoryException {
		throw new ValueFormatException("This is a node not property!");
	}
}
